package com.zhuwhao.sample.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.zhuwhao.sample.R;

/**
 * Created by Juno on 2017/3/10 0010.
 * 多个状态切换的View
 */
public class MultipleStatusView extends RelativeLayout {

    public static final int STATUS_CONTENT = 0;
    public static final int STATUS_EMPTY = 1;
    public static final int STATUS_ERROR = 2;
    public static final int STATUS_LOADING = 3;
    public static final int STATUS_NO_NETWORK = 4;

    private static final int NULL_RESOURCE_ID = -1;

    private View contentView;
    private View emptyView;
    private View errorView;
    private View loadingView;
    private View noNetworkView;

    private int contentViewResId;
    private int emptyViewResId;
    private int errorViewResId;
    private int loadingViewResId;
    private int noNetworkViewResId;

    private int viewStatus;

    private LayoutInflater inflater;
    private OnClickListener onRetryClickListener;

    public MultipleStatusView(Context context) {
        this(context, null);
    }

    public MultipleStatusView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultipleStatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultipleStatusView, defStyleAttr, 0);
        contentViewResId = a.getResourceId(R.styleable.MultipleStatusView_contentView, NULL_RESOURCE_ID);
        emptyViewResId = a.getResourceId(R.styleable.MultipleStatusView_emptyView, R.layout.layout_empty_view);
        errorViewResId = a.getResourceId(R.styleable.MultipleStatusView_errorView, R.layout.layout_error_view);
        loadingViewResId = a.getResourceId(R.styleable.MultipleStatusView_loadingView, R.layout.layout_loading_view);
        noNetworkViewResId = a.getResourceId(R.styleable.MultipleStatusView_noNetworkView, R.layout.layout_no_network_view);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        inflater = LayoutInflater.from(getContext());
        showContent();
    }

    /**
     * 获取当前状态
     *
     * @return int
     */
    public int getViewStatus() {
        return viewStatus;
    }

    /**
     * 设置重试点击事件
     */
    public void setOnRetryClickListener(OnClickListener onRetryClickListener) {
        this.onRetryClickListener = onRetryClickListener;
    }

    private final ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    /**
     * 显示内容视图
     */
    public final void showContent() {
        viewStatus = STATUS_CONTENT;
        if (contentView == null) {
            if (contentViewResId != NULL_RESOURCE_ID) {
                contentView = inflater.inflate(contentViewResId, null);
                addView(contentView, 0, layoutParams);
            } else {
                contentView = findViewById(R.id.contentView);
            }
        }
        showViewByStatus(viewStatus);
    }

    /**
     * 显示空视图
     */
    public final void showEmpty() {
        viewStatus = STATUS_EMPTY;
        if (emptyView == null) {
            emptyView = inflater.inflate(emptyViewResId, null);
            View emptyRetryView = emptyView.findViewById(R.id.emptyRetryView);
            if (onRetryClickListener != null && emptyRetryView != null) {
                emptyRetryView.setOnClickListener(onRetryClickListener);
            }
            addView(emptyView, 0, layoutParams);
        }
        showViewByStatus(viewStatus);
    }

    /**
     * 显示错误视图
     */
    public final void showError() {
        viewStatus = STATUS_ERROR;
        if (errorView == null) {
            errorView = inflater.inflate(errorViewResId, null);
            View errorRetryView = errorView.findViewById(R.id.errorRetryView);
            if (onRetryClickListener != null && errorRetryView != null) {
                errorRetryView.setOnClickListener(onRetryClickListener);
            }
            addView(errorView, 0, layoutParams);
        }
        showViewByStatus(viewStatus);
    }

    /**
     * 显示加载中视图
     */
    public final void showLoading() {
        viewStatus = STATUS_LOADING;
        if (loadingView == null) {
            loadingView = inflater.inflate(loadingViewResId, null);
            addView(loadingView, 0, layoutParams);
        }
        showViewByStatus(viewStatus);
    }

    /**
     * 显示无网络视图
     */
    public final void showNoNetwork() {
        viewStatus = STATUS_NO_NETWORK;
        if (noNetworkView == null) {
            noNetworkView = inflater.inflate(noNetworkViewResId, null);
            View noNetworkRetryView = noNetworkView.findViewById(R.id.noNetworkRetryView);
            if (onRetryClickListener != null && noNetworkRetryView != null) {
                noNetworkRetryView.setOnClickListener(onRetryClickListener);
            }
            addView(noNetworkView, 0, layoutParams);
        }
        showViewByStatus(viewStatus);
    }

    /**
     * 切换视图
     */
    private void showViewByStatus(int viewStatus) {
        if (contentView != null) {
            contentView.setVisibility(viewStatus == STATUS_CONTENT ? View.VISIBLE : View.GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(viewStatus == STATUS_EMPTY ? View.VISIBLE : View.GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(viewStatus == STATUS_ERROR ? View.VISIBLE : View.GONE);
        }
        if (loadingView != null) {
            loadingView.setVisibility(viewStatus == STATUS_LOADING ? View.VISIBLE : View.GONE);
        }
        if (noNetworkView != null) {
            noNetworkView.setVisibility(viewStatus == STATUS_NO_NETWORK ? View.VISIBLE : View.GONE);
        }
    }
}
